package utils;

import model.AloneLinkNode;

/**
 * 单向链表
 */
public class AloneLinkUtil extends AbstractLineClass {
    public AloneLinkUtil() {
        super(0);
    }

    private int findData(AloneLinkNode head, int value, int defaultValue) {
        int site = defaultValue;
        AloneLinkNode currentNode = head;
        int index = 0;
        while (currentNode != null) {
            if (currentNode.getData() == value) {
                site = index;
                break;
            }
            currentNode = currentNode.getAfterNode();
        }
        return site;
    }

    public AloneLinkNode insertDate(AloneLinkNode head, int site, int value) {
        if (!checkSite(site))
            return null;
        AloneLinkNode currentNode = head;
        // TODO: 2023/11/7 将当前位置停止在预计删除的序号的元素
        int index = 0;
        while (currentNode != null && index < site) {
            currentNode = currentNode.getAfterNode();
            index++;
        }
        if (index != site) {
            return null;
        }
        final AloneLinkNode newNode = new AloneLinkNode();
        newNode.setData(value);
        newNode.setAfterNode(currentNode.getAfterNode());
        currentNode.setAfterNode(newNode);
        super.addSize();
        return head;
    }

    public AloneLinkNode updateData(AloneLinkNode head, int site, int updateValue) {
        if (!checkSite(site))
            return null;
        AloneLinkNode currentNode = head;
        // TODO: 2023/11/7 将当前位置停止在预计删除的序号的元素
        int index = 0;
        while (currentNode != null && index < site) {
            currentNode = currentNode.getAfterNode();
            index++;
        }
        if (index != site) {
            return null;
        }
        currentNode.setData(updateValue);
        return head;
    }

    public AloneLinkNode deleteData(AloneLinkNode head, int site) {
        if (!checkSite(site))
            return null;
        AloneLinkNode currentNode = head;
        // TODO: 2023/11/7 将当前位置停止在预计删除的序号的前驱元素
        int index = 0;
        while (currentNode != null && index < site - 1) {
            currentNode = currentNode.getAfterNode();
            index++;
        }
        if (index != site - 1) {
            return null;
        }
        currentNode.setAfterNode(currentNode.getAfterNode().getAfterNode());
        super.subtractionSize();
        return head;
    }

    public boolean destroyLink(AloneLinkNode head) {
        // TODO: 2023/11/7 JAVA中的对象内存，它会自动释放，无需free
        // TODO: 2023/11/7 这里我们以将值改为-1，标识销毁
        boolean resultValue = false;
        if (head != null) {
            do {
                super.subtractionSize();
                head.setData(-1);
                head = head.getAfterNode();
            } while (head != null);
            resultValue = true;
        }
        return resultValue;
    }

    public void printLink(AloneLinkNode head) {
        if (head != null) {
            int index = 0;
            do {
                System.out.println(String.format("序号：%d，值：%d", index++, head.getData()));
                head = head.getAfterNode();
            } while (head != null);
        }
    }

    /**
     * 初始化单向链表，新元素加到最右边
     *
     * @param size
     * @return
     */
    public AloneLinkNode linkInitiate(int size) {
        AloneLinkNode head = null;
        AloneLinkNode preNode = null;
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                final AloneLinkNode newNode = new AloneLinkNode();
                newNode.setData(i);
                preNode.setAfterNode(newNode);
                preNode = newNode;
            } else {
                head = new AloneLinkNode();
                preNode = head;
            }
        }
        super.setSize(size);
        return head;
    }
}
